package com.tianqu.tms.www.car.controller;

import org.apache.commons.lang.StringUtils;

import com.ibm.icu.math.BigDecimal;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.dto.PageResultDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.car.viewhelper.FindCarBrandCountViewHelper;
import com.tianqu.tms.www.car.viewhelper.FindCarBrandListViewHelper;
import com.tianqu.tms.www.car.viewhelper.FindCarBrandViewHelper;
import com.tianqu.tms.www.car.viewhelper.FindCarIndexViewHelper;
import com.tianqu.tms.www.car.viewhelper.FindCarPriceListViewHelper;
import com.tianqu.tms.www.car.viewhelper.FindCarPriceViewHelper;
import com.tianqu.tms.www.car.viewhelper.FindCarRecommendListViewHelper;

public class FindCarController  extends TmsController {

	@Override
	public void execute(UserContext uc) throws Exception {
		throw new Exception("abandoned method");
	}
	
	public FindCarIndexViewHelper FindCarIndexController() throws Exception{
		FindCarIndexViewHelper view = new FindCarIndexViewHelper();
		TxController controller = TxController.getTxController();
		try {
			controller.begin();
			view.setRecommendList(Member.getInstance().findRecommendMember(2));
			view.setOnlineCount(Vendor.getInstance().findOnlineCount());
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException)  throw e;
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		return  view;
	}

	
	public FindCarRecommendListViewHelper FindCarRecommendedListController(String memberId,int pageNumber,int pageSize) throws Exception{
		FindCarRecommendListViewHelper view = new FindCarRecommendListViewHelper();
		TxController controller = TxController.getTxController();
		try {
			controller.begin();
			if(StringUtils.isNotBlank(memberId) && pageNumber > 0){
				Member member = new Member();
				member = member.getMember(memberId);
				//
				Car car = new Car();
				PageResultDto dto = car.findPageCarByMemberId(memberId, pageNumber, pageSize);
				//
				int page = new BigDecimal(dto.getRecordCount()).divide( new BigDecimal(pageSize),BigDecimal.ROUND_UP).intValue();
				view.setMemberId(memberId);
				view.setPageNumber(pageNumber);
				view.setPageSize(pageSize);
				view.setRecordCount(dto.getRecordCount());
				view.setResult(dto.getResult());
				view.setNextPage(pageNumber+1);
				view.setPreviousPage(pageNumber-1);
				view.setTotalPage(page);
				if(dto.isHasNextPage()) view.setHasNextPage(1);
				else  view.setHasNextPage(0);
				view.setShortName(member.getShortName());
				view.setRecommend(member.getRecommend());
			}
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException)  throw e;
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		return  view;
	}
	 
	
	public FindCarBrandViewHelper FindCarBrandListController(int pageNumber,int pageSize) throws Exception{
		FindCarBrandViewHelper view = new FindCarBrandViewHelper();
		TxController controller = TxController.getTxController();
		try {
			controller.begin(); 
			PageResultDto dto = Vendor.getInstance().getPage(pageSize, pageNumber);
			int page = new BigDecimal(dto.getRecordCount()).divide( new BigDecimal(32),BigDecimal.ROUND_UP).intValue();
			if(dto.isHasNextPage()) view.setHasNextPage(1);
			else  view.setHasNextPage(0);
			//
			view.setNextPage(pageNumber + 1);
			view.setPreviousPage(pageNumber - 1);
			view.setPageNumber(pageNumber);
			view.setPageSize(pageSize);
			view.setRecordCount(dto.getRecordCount());
			view.setResult(dto.getResult());
			view.setTotalPage(page);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException)  throw e;
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		return  view;
	}
	
	
	public FindCarPriceViewHelper FindCarPriceController() throws Exception{
		TxController controller = TxController.getTxController();
		FindCarPriceViewHelper view = new FindCarPriceViewHelper();
		try {
			controller.begin(); 
			view.setRecommendList(Member.getInstance().findRecommendMember(2));
			view.setResult(Vendor.getInstance().getMarketCarPriceCount());
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException)  throw e;
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		return view;
	}
	
	public FindCarBrandCountViewHelper  FindCarBrandCountListController(String vendorId,int pageNumber,int pageSize) throws Exception{
		TxController controller = TxController.getTxController();
		FindCarBrandCountViewHelper view = new FindCarBrandCountViewHelper();
		try {
			controller.begin(); 
			PageResultDto page = Vendor.getInstance().getPageCarStyleCount(vendorId, pageSize, pageNumber);
			//
			int totalPage = new BigDecimal(page.getRecordCount()).divide( new BigDecimal(pageSize),BigDecimal.ROUND_UP).intValue();
			view.setPageNumber(pageNumber);
			view.setPageSize(pageSize);
			view.setRecordCount(page.getRecordCount());
			view.setResult(page.getResult());
			view.setNextPage(pageNumber+1);
			view.setPreviousPage(pageNumber-1);
			view.setTotalPage(totalPage);
			view.setVendorId(vendorId);
			view.setVendorName(Vendor.getInstance().getVendorInfo(vendorId).getVendorName());
			if(page.isHasNextPage()) view.setHasNextPage(1);
			else  view.setHasNextPage(0);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException)  throw e;
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		return view;
	}
	
	public FindCarBrandListViewHelper  FindCarBrandListController(String carStyleId,int pageNumber,int pageSize) throws Exception{
		return FindCarBrandListController("",carStyleId,pageNumber,pageSize);
	}
	
	public FindCarBrandListViewHelper  FindCarBrandListController(String vendorId,String carStyleId,int pageNumber,int pageSize) throws Exception{
		TxController controller = TxController.getTxController();
		FindCarBrandListViewHelper view = new FindCarBrandListViewHelper();
		try {
			controller.begin(); 
			PageResultDto page = Car.getInstance().findPageCarByCarStyleId(carStyleId,pageNumber,pageSize);
			//
			int totalPage = new BigDecimal(page.getRecordCount()).divide( new BigDecimal(pageSize),BigDecimal.ROUND_UP).intValue();
			view.setPageNumber(pageNumber);
			view.setPageSize(pageSize);
			if(!StringUtils.isNotBlank(vendorId)) view.setVendorName(Vendor.getInstance().getVendorInfo(vendorId).getVendorName());
			view.setStyleName(CarStyle.getInstance().getCarStyleInfo(carStyleId).getCarStyleName());
			view.setRecordCount(page.getRecordCount());
			view.setResult(page.getResult());
			view.setNextPage(pageNumber+1);
			view.setPreviousPage(pageNumber-1);
			view.setTotalPage(totalPage);
			view.setCarStyleId(carStyleId);
			view.setVendorId(vendorId);
			if(page.isHasNextPage()) view.setHasNextPage(1);
			else  view.setHasNextPage(0);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException)  throw e;
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		return view;
	}
	
	public FindCarPriceListViewHelper  FindCarPriceListController(double minPrice,double maxPrice, int pageNumber, int pageSize) throws Exception{
		TxController controller = TxController.getTxController();
		FindCarPriceListViewHelper view = new FindCarPriceListViewHelper();
		try {
			controller.begin(); 
			PageResultDto page = Car.getInstance().findMarketCarPrice(minPrice,maxPrice,pageNumber,pageSize);
			int totalPage = new BigDecimal(page.getRecordCount()).divide( new BigDecimal(pageSize),BigDecimal.ROUND_UP).intValue();
			view.setPageNumber(pageNumber);
			view.setPageSize(pageSize);
			view.setRecordCount(page.getRecordCount());
			view.setResult(page.getResult());
			view.setNextPage(pageNumber+1);
			view.setPreviousPage(pageNumber-1);
			view.setTotalPage(totalPage);
			if(page.isHasNextPage()) view.setHasNextPage(1);
			else  view.setHasNextPage(0);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException)  throw e;
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		return view;
	}
}
